import java.util.List;

public class MySingleList implements IList{
    //链表是由一个个节点组成的，每一个节点都是一个完整的对象
    //我们可以把节点用内部类构成
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            //我们不用初始化地址
            this.val = val;
        }
    }
    //我们的MySingleList类的里面要有一个头节点的类
    public ListNode head;

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("");
    }

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);

        newNode.next = this.head;
        this.head = newNode;
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (this.head == null) {
            this.head = newNode;
            return;
        }
        //找尾巴
        ListNode cur = this.head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }
    private void indexExeption(int index) throws IndexException{
        int size = size();
        if (index < 0 || index > size) {
            throw new IndexException("index的位置错误");
        }
    }

    @Override
    public void addIndex(int index, int data) {
        try {
            indexExeption(index);
            if (index == 0) {
                addFirst(data);
            }
            if (index == size()) {
                addLast(data);
            }
            //中间插入
            ListNode cur = this.head;
            while(index - 1 != 0) {
                index--;
                cur = cur.next;
            }
            ListNode newNode = new ListNode(data);
            newNode.next = cur.next;
            cur.next = newNode;
        }catch (IndexException e) {
            System.out.println("index位置异常");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    private ListNode findkeyListNode(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = findkeyListNode(key);
        if (cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }
}
